package com.yuyou.fn.tradingservice.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.tradingservice.entity.ShippingAddress;
import com.yuyou.fn.tradingservice.mapper.ShippingAddressMapper;
import com.yuyou.fn.tradingservice.service.IShippingAddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ShippingAddress 表数据服务层接口实现类
 */
@Service
public class ShippingAddressServiceImpl extends SuperServiceImpl<ShippingAddressMapper, ShippingAddress, Long> implements IShippingAddressService {


    @Override
    public void saveOrUpdateAddress(ShippingAddress shippingAddress) {

        if (StringUtils.isEmpty(shippingAddress.getPersonAddress()) || StringUtils.isEmpty(shippingAddress.getPhoneNo()) || StringUtils.isEmpty(shippingAddress.getConsignee())){
            throw new BusException("缺乏必填参数");
        }
        if (shippingAddress.getAddressId() == null) {
            shippingAddress.setCreateTime(TimeUtils.now());
            shippingAddress.setUpdateTime(TimeUtils.now());
            this.insertSelective(shippingAddress);
        } else {
            shippingAddress.setUpdateTime(TimeUtils.now());
            this.updateSelectiveById(shippingAddress);
        }
    }

    @Override
    public List<ShippingAddress> findAddressListByUserId(Long userId) {
        /*ShippingAddress shippingAddress = new ShippingAddress();
        shippingAddress.setSellerId(userId);*/
        Example<ShippingAddress> example = ShippingAddress.createExample();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        return findListByExample(example);
    }
}